package com.anolesoft.fourteenWaiXie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.fourteenWaiXie.dto.QuantityLogDTO;
import com.anolesoft.fourteenWaiXie.dto.QuantityManagementDTO;
import com.anolesoft.fourteenWaiXie.mapper.QuantityServiceMapper;
import com.anolesoft.fourteenWaiXie.service.QuantityService;
import com.anolesoft.yr.util.CommonUtils;
import com.anolesoft.yr.util.PageData;
import com.anolesoft.yr.util.SecurityUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class QuantityServiceImpl implements QuantityService {

    @Autowired
    QuantityServiceMapper quantityServiceMapper;

    @Override
    public QuantityManagementDTO queryQuantity(String fabrecordid) {
        QuantityManagementDTO quantityManagementDTO = quantityServiceMapper.queryQuantity(fabrecordid);
        return quantityManagementDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateQuantity(QuantityManagementDTO quantityManagementDTO) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        quantityServiceMapper.updateQuantity(quantityManagementDTO);
        int count = quantityServiceMapper.insertQuantityLog(QuantityLogDTO.builder()
                .fabrecordid(CommonUtils.stringNull(quantityManagementDTO.getFabrecordid()))
                .assignmentId(CommonUtils.stringNull(quantityManagementDTO.getAssignmentId()))
                .jx(CommonUtils.stringNull(quantityManagementDTO.getJx()))
                .partId(CommonUtils.stringNull(quantityManagementDTO.getPartId()))
                .pcjc(CommonUtils.stringNull(quantityManagementDTO.getPcjc()))
                .total(CommonUtils.stringNull(quantityManagementDTO.getTotal()))
                .newTotal(CommonUtils.stringNull(quantityManagementDTO.getNewTotal()))
                .operationUid(loginUser.getEmpUid())
                .factory_id(loginUser.getFacCode())
                .newAssignmentId(CommonUtils.stringNull(quantityManagementDTO.getNewAssignmentId()))
                .build());
        return count;
    }

    @Override
    public PageData<List<QuantityLogDTO>> queryQuantityLog(QuantityLogDTO quantityLog) {
        Page<QuantityLogDTO> page = new Page<>(quantityLog.getPageNo(),quantityLog.getPageSize());
        List<QuantityLogDTO> quantityLogDTOS = quantityServiceMapper.queryQuantityLog(quantityLog,page);
        return new PageData<>((int)page.getTotal(),quantityLogDTOS);
    }
}
